<?php
namespace app\admin\model;

use think\Db;
use think\Model;

class SmsReview extends Model
{
    public static function getListAll($request)
    {
        //检测表是否存在
        $tableName = 'bus_review_order_' . $request['st'];
        // return Db::table($tableName)->select();

        $rableResult = Db::query('SHOW TABLES LIKE ' . "'" . $tableName . "'");

        if ($rableResult && $request['cid'] != '') {
            // $where[] = ['o.status', 'eq', 0];
            // if (!empty($request['cid'])) {
            $where[] = ['o.cid', 'eq', $request['cid']];
            // }
            if ($request['phone_type'] != '') {
                $where[] = ['o.phone_type', 'eq', $request['phone_type']];
            }

            if ($request['create_time'] != '') {
                $where[] = ['o.create_time', '> time', $request['create_time']];
            }

            if ($request['end_time'] != '') {
                $where[] = ['o.create_time', '< time', $request['end_time']];
            }
            if ($request['state'] != '') {
                $where[] = ['o.status', 'eq', $request['state']];
            }

            $data = Db::table($tableName)->alias('o')->field('o.*,c.company_name')->join('bus_channel c', 'o.cid = c.channel_id')->where($where)
                ->paginate($request['limit'], false, $request)
                ->toarray();

            foreach ($data['data'] as $k => $v) {
                switch ($v['result']) {
                    case '0':
                        $data['data'][$k]['result'] = '<span class="layui-badge layui-bg-orange status-jy">等待处理</span>';
                        break;
                    case '1':
                        $data['data'][$k]['result'] = '<span class="layui-badge layui-bg-green status-qy">已处理</span>';
                        break;
                    default:
                        $data['data'][$k]['result'] = '未定义';
                        break;
                }
                switch ($v['phone_type']) {
                    case '0':
                        $data['data'][$k]['phone_type'] = '<span class="layui-badge layui-bg-blue status-jy">群发</span>';
                        break;
                    case '1':
                        $data['data'][$k]['phone_type'] = '<span class="layui-badge layui-bg-green status-qy">单发</span>';
                        break;
                    default:
                        $data['data'][$k]['phone_type'] = '未定义';
                        break;
                }
                switch ($v['status']) {
                    case '0':
                        $data['data'][$k]['status'] = '<span class="layui-badge layui-bg-orange status-jy">待审核</span>';
                        break;
                    case '1':
                        $data['data'][$k]['status'] = '<span class="layui-badge layui-bg-green status-qy">通过</span>';
                        break;
                    case '2':
                        $data['data'][$k]['status'] = '<span class="layui-badge status-qy">失败</span>';
                        break;
                    default:
                        $data['data'][$k]['status'] = '未定义';
                        break;
                }
                switch ($v['submit_type']) {
                    case '0':
                        $data['data'][$k]['submit_type'] = '<span class="layui-badge layui-bg-orange status-jy">http</span>';
                        break;
                    case '1':
                        $data['data'][$k]['submit_type'] = '<span class="layui-badge layui-bg-cyan status-qy">cmpp</span>';
                        break;
                    default:
                        $data['data'][$k]['submit_type'] = '未定义';
                        break;
                }
                if ($v['phone_size'] == '') {
                    $data['data'][$k]['phone_size'] = '无号码';
                } else {
                    // $data['data'][$k]['phone_size'] = '<a href="#">' . $v['phone_size'] . '</a>';
                    $data['data'][$k]['phone_size'] = '<a href="javascript:;" onclick="open_window(\'查看号码\',\'/admin/sms_review/smsPhone.html\',\'?table_order_id=' . $v['id'] . '&st=' . $request['st'] . '\',\'600\',\'400\')" style="color: #3498db;">' . $v['phone_size'] . '</a>';
                }
                $data['data'][$k]['company_name'] = $data['data'][$k]['company_name'] . '(' . $data['data'][$k]['cid'] . ')';
            }
        } else {
            $data['total'] = 0;
            $data['data']  = [];
        }

        return $data;

    }

    /**
     * [reviewSms 审核短信]
     * @param  [type] $request [description]
     * @return [type]          [description]
     */
    public static function reviewSms($request)
    {
        //检测表是否存在
        $tableName = 'bus_review_order_' . $request['st'];

        $rableResult = Db::query('SHOW TABLES LIKE ' . "'" . $tableName . "'");

        if ($rableResult) {
            $where[] = ['status', 'eq', 0];
            $where[] = ['id', 'In', json_decode($request['reviewId'], true)];
            $where[] = ['cid', 'eq', $request['cid']];
            $batchId = uniqid();
            $data    = [
                'desc'        => $request['desc'],
                'status'      => $request['status'],
                'batch_id'    => $batchId,
                'review_time' => date('Y-m-d H:i:s'),
                'username'    => session('username'),
            ];
            Db::startTrans();
            try {
                $tableResult = Db::table($tableName)->where($where)->update($data);
                if ($tableResult != false) {
                    $url         = 'http://183.240.150.13:3389/SMS/review/';
                    $requestData = [
                        'tableDate' => $request['st'],
                        'cid'       => $request['cid'],
                        'batchId'   => $batchId,
                        'type'      => $request['phone_type'],
                    ];
                    trace('短信审核请求java--' . json_encode($requestData));
                    $javaResult = curl_post($url, $requestData);
                    trace('请求返回---' . json_encode($javaResult));
                }
                // 提交事务
                Db::commit();
                $result = ['code' => 200, 'msg' => '处理成功'];
                //请求java
            } catch (\Exception $e) {
                trace('短信审核报错---' . $e);
                // 回滚事务
                Db::rollback();
                $result = ['code' => 422, 'msg' => '处理失败'];
            }

        } else {
            $result = ['code' => 422, 'msg' => '不存在该数据表'];
        }

        return $result;

    }

    /**
     * [getReviewList 获取所有审核短信]
     * @param  [type] $request [description]
     * @return [type]          [description]
     */
    public static function getReviewList($request)
    {
        //检测表是否存在
        $tableName = 'bus_review_order_' . $request['st'];
        // return Db::table($tableName)->select();

        $rableResult = Db::query('SHOW TABLES LIKE ' . "'" . $tableName . "'");

        if ($rableResult) {
            $where = [];
            // $where[] = ['status', 'eq', 0];
            if (!empty($request['cid'])) {
                $where[] = ['o.cid', 'eq', $request['cid']];
            }
            if ($request['phone_type'] != '') {
                $where[] = ['o.phone_type', 'eq', $request['phone_type']];
            }
            if ($request['create_time'] != '') {
                $where[] = ['o.create_time', '> time', $request['create_time']];
            }

            if ($request['end_time'] != '') {
                $where[] = ['o.create_time', '< time', $request['end_time']];
            }

            $data = Db::table($tableName)->alias('o')->field('o.*,c.company_name')->join('bus_channel c', 'o.cid = c.channel_id')->where($where)
                ->paginate($request['limit'], false, $request)
                ->toarray();

            foreach ($data['data'] as $k => $v) {
                switch ($v['result']) {
                    case '0':
                        $data['data'][$k]['result'] = '<span class="layui-badge status-jy">等待处理</span>';
                        break;
                    case '1':
                        $data['data'][$k]['result'] = '<span class="layui-badge layui-bg-green status-qy">已处理</span>';
                        break;
                    default:
                        $data['data'][$k]['result'] = '未定义';
                        break;
                }
                switch ($v['phone_type']) {
                    case '0':
                        $data['data'][$k]['phone_type'] = '<span class="layui-badge layui-bg-blue status-jy">群发</span>';
                        break;
                    case '1':
                        $data['data'][$k]['phone_type'] = '<span class="layui-badge layui-bg-green status-qy">单发</span>';
                        break;
                    default:
                        $data['data'][$k]['phone_type'] = '未定义';
                        break;
                }
                switch ($v['status']) {
                    case '0':
                        $data['data'][$k]['status'] = '<span class="layui-badge status-jy">待审核</span>';
                        break;
                    case '1':
                        $data['data'][$k]['status'] = '<span class="layui-badge layui-bg-green status-qy">通过</span>';
                        break;
                    case '2':
                        $data['data'][$k]['status'] = '<span class="layui-badge layui-bg-green status-qy">失败</span>';
                        break;
                    default:
                        $data['data'][$k]['status'] = '未定义';
                        break;
                }
                switch ($v['submit_type']) {
                    case '0':
                        $data['data'][$k]['submit_type'] = '<span class="layui-badge layui-bg-orange status-jy">http</span>';
                        break;
                    case '1':
                        $data['data'][$k]['submit_type'] = '<span class="layui-badge layui-bg-cyan status-qy">cmpp</span>';
                        break;
                    default:
                        $data['data'][$k]['submit_type'] = '未定义';
                        break;
                }
                if ($v['phone_size'] == '') {
                    $data['data'][$k]['phone_size'] = '无号码';
                } else {
                    $data['data'][$k]['phone_size'] = '<a href="javascript:;" onclick="open_window(\'查看号码\',\'/admin/sms_review/smsPhone.html\',\'?table_order_id=' . $v['id'] . '&st=' . $request['st'] . '\',\'600\',\'400\')" style="color: #3498db;">' . $v['phone_size'] . '</a>';
                    // <a href="javascript:;" onclick="open_window('查看号码','/admin/platmage/platmage_check.html','?pid=119','600','400')" style="color: #3498db;"> <u>CMPP客户端1号</u> </a>
                }
                $data['data'][$k]['company_name'] = $data['data'][$k]['company_name'] . '(' . $data['data'][$k]['cid'] . ')';
            }
        } else {
            $data['total'] = 0;
            $data['data']  = [];
        }

        return $data;

    }
}
